<?php

class ProvinsisController extends \BaseController {

	/**
	 * Display a listing of provinsis
	 *
	 * @return Response
	 */

	public function dataTable()
	{
		$provinsis = DB::table('provinsis')->where('provinsis.is_active', '=', '1')->select(array('provinsis.id','provinsis.nama','provinsis.keterangan'));
		return Datatables::of($provinsis)
		->add_column('operations', '<div class="btn-group">
                                        <a href="#" class="btn btn-default btn-detail" data-nama="{{ $nama }}" data-keterangan="{{ $keterangan }}"><i class="fa fa-list text-danger" data-toggle="tooltip" data-placement="top" data-original-title="Detail"></i></a>
                                        {{ HTML::decode(link_to_route(\'provinsis.edit\', \'<i class="fa fa-pencil text-warning" data-toggle="tooltip" data-placement="top" data-original-title="Edit"></i>\', $id, array(\'class\'=>\'btn btn-default btn-edit\'))) }}
                                        <a href="#" class="btn btn-default btn-delete" data-form=\'{{Form::model($id,array(\'route\'=>array(\'provinsis.destroy\',$id),\'method\'=>\'DELETE\',\'class\'=>\'formDelete\'))}}{{Form::close()}}\'><i class="fa fa-times text-danger" data-toggle="tooltip" data-placement="top" data-original-title="Delete"></i></a>
	                                </div>')
		->make();
	}

	public function index()
	{
		// $provinsis = Provinsi::where('is_active', '=', '1')->orderBy('nama','ASC')->get();

		return View::make('provinsis.index');
	}

	/**
	 * Show the form for creating a new provinsi
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('provinsis.create');
	}

	/**
	 * Store a newly created provinsi in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$validator = Validator::make($data = Input::all(), Provinsi::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Provinsi::create($data);

		return Redirect::route('provinsis.index');
	}

	/**
	 * Display the specified provinsi.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		$provinsi = Provinsi::findOrFail($id);

		return View::make('provinsis.show', compact('provinsi'));
	}

	/**
	 * Show the form for editing the specified provinsi.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$provinsi = Provinsi::find($id);

		return View::make('provinsis.edit', compact('provinsi'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$validator = Validator::make($data = Input::all(), Provinsi::$rules);

		if ($validator->fails())
		{
			return Redirect::back()->withErrors($validator)->withInput();
		}

		Provinsi::create($data);

		$provinsi = Provinsi::findOrFail($id);

		// $validator = Validator::make($data = Input::all(), Provinsi::$rules);

		// if ($validator->fails())
		// {
		// 	return Redirect::back()->withErrors($validator)->withInput();
		// }

		// $provinsi->update($data);

		$provinsi->is_active = 0;

		$provinsi->save();

		return Redirect::route('provinsis.index');
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// Provinsi::destroy($id);

		$provinsi = Provinsi::findOrFail($id);

		$provinsi->is_active = 0;
		$provinsi->user_id = Auth::user()->id;
		$provinsi->action = 3;

		$provinsi->save();

		return Redirect::route('provinsis.index');
	}

	public function getComboViewKabupatens()
	{
		$id_provinsi       = Input::get('id_provinsi');
		$provinsi          = Provinsi::findOrFail($id_provinsi);
		$kabupatens        = $provinsi->kabupatens;		
		
		if(!is_null(Lingkungan::active()->first())) {
			$lingkungan        = Lingkungan::active()->first()->toArray();    			

			$current_kabupaten = $lingkungan['kabupaten_id'];

			return View::make('provinsis.partials.combo_view_kabupatens')
					->with(compact('kabupatens'))
					->with(compact('current_kabupaten'));	
		}else{
			return View::make('provinsis.partials.combo_view_kabupatens')
					->with(compact('kabupatens'));					
		}			
	}

}